# packages
library(tidyverse) # ecosystem of data science packages
library(spData) # data set for spatial analysis
library(magick) # for gif animationHW10: Graphics of Storm Tracks in the North Atlantic
Week 10: Iterations and Loops
1 Animated Visualization with GIF files
In this assignment you will create a couple of GIF files to visualize storm tracks in the North Atlantic. This is related to what you did in HW6 but this time you won’t use "gganimate", instead you will produce GIF images with some functions from the package "magick".
storms# A tibble: 19,537 × 13
name year month day hour lat long status category wind pressure
<chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <fct> <dbl> <int> <int>
1 Amy 1975 6 27 0 27.5 -79 tropical d… NA 25 1013
2 Amy 1975 6 27 6 28.5 -79 tropical d… NA 25 1013
3 Amy 1975 6 27 12 29.5 -79 tropical d… NA 25 1013
4 Amy 1975 6 27 18 30.5 -79 tropical d… NA 25 1013
5 Amy 1975 6 28 0 31.5 -78.8 tropical d… NA 25 1012
6 Amy 1975 6 28 6 32.4 -78.7 tropical d… NA 25 1012
7 Amy 1975 6 28 12 33.3 -78 tropical d… NA 25 1011
8 Amy 1975 6 28 18 34 -77 tropical d… NA 30 1006
9 Amy 1975 6 29 0 34.4 -75.8 tropical s… NA 35 1004
10 Amy 1975 6 29 6 34 -74.8 tropical s… NA 40 1002
# ℹ 19,527 more rows
# ℹ 2 more variables: tropicalstorm_force_diameter <int>,
# hurricane_force_diameter <int>
2 Your Turn: Making Individual Graphics for Years 2000-2022
Your goal is to make and export individual graphics of storm tracks for every year between 2000 and 2022.
Graph maps of storm tracks making sure that the visual appearance of each graphic is okay. In other words, the quality of the graphics should be okay to share with a general audience, with your boss, clients, on social media, etc.
Write a
forloop to export the images as PNG files.Export each PNG file using
ggsave(), specifyingwidth = 6andheight = 4in inches.Hint: the function
paste0()can help you in the creation of the file names: e.g. “storm_tracks_2000.png”, “storm_tracks_2001.png”, …, “storm_tracks_2022.png”
for (yr in 2000:2022) {
ggplot(data = filter(storms, year == yr),
mapping = aes(x = lat, y = long, group = name, color = name)) + geom_point(size = 1, alpha = 0.3) + labs(title = yr)
ggsave(paste0("storm_tracks_", yr, ".png"), width = 6, height = 4)
}
3 Your Turn: Creating a GIF from exported PNGs
Once you have the set of PNG files (one PNG per year), the next stage involves importing them in R to create the GIF image. Follow the steps listed below to accomplish this task:
Create a character vector
pngswith the names of the PNG files.- BTW: Make sure the PNG files are in the same directory of your qmd file!!!
Run the command
png_list <- lapply(pngs, image_read)to import all the PNG files into a list.Apply the
image_join()function to the listpng_listto combine all the PNG files into a single image. Call the output objectimg_joined, which is amagickimage object.Apply the function
image_animate()to your magick image objectimg_joined. This will create the animation. Call the output objectimg_animated. You may want to test different values for the argumentsfpsanddelay, e.gfps = 2, delay = 100, depending on how fast or slow you want the GIF to be played.Finally, export your object
img_animatedto a GIF file with the functionimage_write(). You can call the exported filestorm-tracks.gifor something like that.
pngs <- c()
for (yr in 2000:2022) {
pngs <- c(pngs, paste0("storm_tracks_", yr, ".png"))
}
png_list <- lapply(pngs, image_read)
img_joined <- image_join(png_list)
img_animated <- image_animate(img_joined, fps = 2, delay = 100)
image_write(img_animated, "storm-tracks.gif")img_animated
4 Your Turn: Rinse and Repeat
Make another GIF animation to visualize storm tracks of your choice, based on at least 10 individual graphics.
For instance, you can focus on tropical cyclones in certain months, or also focus on certain kind of storms (hurricanes in general, or of a certain category).
Use this opportunity to practice your tidyverse skills for manipulating data, and making beautiful graphics.
Important: For the creation of the PNG files, do this with a while loop instead of a for-loop.
yr <- 1975
while (yr <= 2020) {
ggplot(data = filter(storms, year == yr & status == "tropical storm"),
mapping = aes(x = lat, y = long, group = name, color = name)) + geom_point(size = 1, alpha = 0.3) + labs(title = yr)
ggsave(paste0("tropicals", yr, ".png"), width = 6, height = 4)
yr <- yr + 1
}
pngs1 <- c()
for (yr in 1975:2020) {
pngs1 <- c(pngs1, paste0("tropicals", yr, ".png"))
}
png_list1 <- lapply(pngs1, image_read)
img_joined1 <- image_join(png_list1)
img_animated1 <- image_animate(img_joined1, fps = 2, delay = 100)
image_write(img_animated1, "tropicals.gif")img_animated1